* Replication Code: Did the Budapest Convention Deter Cybercrime? An Event Study Using a Panel of Countries
* Authors: Humza Khan and Pengfei Zhang
* Data: DDoS_attacks

* Install programs
ssc install reghdfe
ssc install event_plot
ssc install did_imputation
ssc install csdid
ssc install drdid
ssc install ivreg2
ssc install avar
ssc install eventstudyinteract
ssc install did_multiplegt

* Import data 

** converts strings (characters/text) to numeric
destring, replace

*unique numeric identifier for each country
gen country_num = _n 

*Reshape the data from wide to long format
reshape long attack gdp_ int_ edu_, i(country_num) j(year)

*dataset as a time series dataset 
tsset country_num year   

*Create the treatment variable, i.e., timeToTreat 
gen timeToTreat = year - eventtime 
gen treatment = cond(year >= eventtime,1,0)

* preserve the dataset for data analysis manipulation
preserve

*1 OLS regression
regress attack treatment

*2 country fixed effect
xtreg attack treatment, fe

*3 twfe
reghdfe attack treatment, absorb(country_num year) cluster(country_num)

*4 twfe plus controls
reghdfe attack treatment gdp_ int_ edu_, absorb(country_num year)

*Dynamic Regression  
reghdfe attack F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols  

* drop observations based on time window
drop if timeToTreat < -10
drop if timeToTreat > 10
 
* bc event windows
forvalues l = 0/10 {  
    gen L`l'event = timeToTreat == `l'
} 
forvalues l = 1/10 {   // now only keep up to F7event
    gen F`l'event = timeToTreat == -`l' 
} 
drop F1event

* Dynamic Regression (same as model 4, different code to run the plots)
reghdfe attack F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols  

********* full event plot ols
event_plot ols, ///
    stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
    graph_opt(ytitle("Estimated Change in DDoS Attacks") ylabel(, labsize(small)) xlabel(-7(1)16, labsize(small)) xtitle("Years Before and After Ratification") ///
        legend(off) ///
        xline(-1, lcolor(gs13) lpattern(dash)) yline(0, lcolor(gs13)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
    ) ///
    lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black))	
	
******************************************
*** Graph: DiD Estimator Comparisons ***
******************************************

*** Borusyak et al. ***
did_imputation attack country_num year eventtime, autosample minn(0) allhorizons pretrends(17) cluster(country_num) 
estimates store bjs  
 
*** Callaway and San'Anna *** SHOWS ALL ZERO VALUES
gen gvar = cond(eventtime==., 0, eventtime)  
csdid attack, ivar(country_num) time(year) gvar(gvar) notyet event
estat event, estore(cs)  

*** Sun and Abraham ***
gen lastcohort = eventtime==r(max)
eventstudyinteract attack L*event F*event, vce(cluster country_num) absorb(country_num year) cohort(eventtime) control_cohort(lastcohort)
matrix sa_b = e(b_iw)  
matrix sa_v = e(V_iw)

*** de Chaisemartin-D'Haultfoeuille ***
did_multiplegt_old attack country_num year treatment, robust_dynamic dynamic(11) placebo(4) breps(100) cluster(country_num)

matrix dcdh_b = e(estimates)
matrix dcdh_v = e(variances)

*HTE estimator graph
event_plot bjs dcdh_b#dcdh_v cs sa_b#sa_v ols, ///
	stub_lag(tau# Effect_# Tp# L#event L#event) stub_lead(pre# Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.325(0.13)0.325) trimlead(10) trimlag(10) noautolegend ///
	graph_opt( ///
		xtitle("Years Before and After Ratification",size(small)) ytitle("Estimated Change in DDoS Attacks",size(small)) xlabel(-10(1)10, labsize(vsmall)) ylabel(-2(1)10, labsize(vsmall)) ///
		legend(order(1 "Borusyak et al." 3 "de Chaisemartin-D'Haultfoeuille" ///
				5 "Callaway-Sant'Anna" 7 "Sun-Abraham" 9 "OLS") rows(2) position(6) region(lcolor(black) lpattern(longdash)) size(small) placement(center) lalign(center)) ///
		xline(-1, lcolor(gs12) lpattern(dash)) yline(0, lpattern(dash) lcolor(gs12)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal) labsize(vsmall)) ///
	) ///
	lag_opt1(msymbol(+) color(red)) lag_ci_opt1(color(red)) ///
	lag_opt2(msymbol(O) color(orange)) lag_ci_opt2(color(orange)) ///
	lag_opt3(msymbol(Dh) color(blue)) lag_ci_opt3(color(blue)) ///
	lag_opt4(msymbol(Th) color(emerald)) lag_ci_opt4(color(emerald)) ///
	lag_opt5(msymbol(Sh) color(black)) lag_ci_opt5(color(black)) 




